/*
 * Created by JFormDesigner on Thu Nov 18 16:40:36 CST 2021
 */

import Factory.Zh0202CalculateFactory;
import Calculator.Zh0202Calculate;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class Calculator extends JFrame {
    public Calculator() {
        initComponents();
    }

    private String zh = "";
    private Double x;
    private Double y;
    private int Symbol = -1;

    private void btn1MouseClicked(MouseEvent e) {
        zh = zh + 1;
        txtIn.setText(zh);
    }

    private void btn2MouseClicked(MouseEvent e) {
        zh = zh + 2;
        txtIn.setText(zh);
    }

    private void btn3MouseClicked(MouseEvent e) {
        zh = zh + 3;
        txtIn.setText(zh);
    }

    private void btn4MouseClicked(MouseEvent e) {
        zh = zh + 4;
        txtIn.setText(zh);
    }

    private void btn5MouseClicked(MouseEvent e) {
        zh = zh + 5;
        txtIn.setText(zh);
    }

    private void btn6MouseClicked(MouseEvent e) {
        zh = zh + 6;
        txtIn.setText(zh);
    }

    private void btn7MouseClicked(MouseEvent e) {
        zh = zh + 7;
        txtIn.setText(zh);
    }

    private void btn8MouseClicked(MouseEvent e) {
        zh = zh + btn8.getText();
        txtIn.setText(zh);
    }

    private void btn9MouseClicked(MouseEvent e) {
        zh = zh + 9;
        txtIn.setText(zh);
    }

    private void btnAddMouseClicked(MouseEvent e) {
        x = Double.valueOf(zh);
        txtIn.setText("");
        Symbol = 0;
        zh="";

    }

    private void btnCalculateMouseClicked(MouseEvent e) {
        y = Double.valueOf(zh);
        Zh0202Calculate calculate;
        Zh0202CalculateFactory factory;
        factory= (Zh0202CalculateFactory) XMLUtil.getBean(Symbol);
        calculate=factory.produceCalculate();
        Double res=calculate.calculate(x,y);
        String result = res.toString();
        txtOut.setText(result);
    }

    private void btnSubMouseClicked(MouseEvent e) {
        x = Double.valueOf(zh);
        txtIn.setText("");
        Symbol = 1;
        zh="";
    }

    private void btnMulMouseClicked(MouseEvent e) {
        x = Double.valueOf(zh);
        txtIn.setText("");
        Symbol = 2;
        zh="";
    }

    private void btnDivMouseClicked(MouseEvent e) {
        x = Double.valueOf(zh);
        txtIn.setText("");
        Symbol = 3;
        zh="";
    }

    private void btnPointMouseClicked(MouseEvent e) {
        zh = zh + ".";
        txtIn.setText(zh);
    }

    private void btnDel2MouseClicked(MouseEvent e) {
        txtIn.setText("");
        txtOut.setText("");
        x=0.0;
        y=0.0;
        zh="";

    }


    private void btnLogMouseClicked(MouseEvent e) {
        x = Double.valueOf(zh);
        txtIn.setText("");
        Symbol = 4;
        zh="";
    }
    

    private void btnDelMouseClicked(MouseEvent e) {
        zh=zh.substring(0,zh.length()-1);
        txtIn.setText(zh);
    }

    private void btn0MouseClicked(MouseEvent e) {
        zh = zh + "0";
        txtIn.setText(zh);
    }

    private void btnPowMouseClicked(MouseEvent e) {
        x = Double.valueOf(zh);
        txtIn.setText("");
        Symbol = 5;
        zh="";
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        btn1 = new JButton();
        btn4 = new JButton();
        btn6 = new JButton();
        btn7 = new JButton();
        btn8 = new JButton();
        btn9 = new JButton();
        btn5 = new JButton();
        btn2 = new JButton();
        btn3 = new JButton();
        btnLog = new JButton();
        btn0 = new JButton();
        btnPoint = new JButton();
        btnDel = new JButton();
        btnSub = new JButton();
        btnAdd = new JButton();
        btnCalculate = new JButton();
        btnMul = new JButton();
        btnDiv = new JButton();
        btnPow = new JButton();
        txtOut = new JTextField();
        txtIn = new JTextField();
        btnDel2 = new JButton();
        this2 = new JFrame();
        button4 = new JButton();
        button8 = new JButton();
        button14 = new JButton();
        button15 = new JButton();
        button16 = new JButton();
        button17 = new JButton();
        button18 = new JButton();
        button5 = new JButton();
        button6 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();

        //---- btn1 ----
        btn1.setText("1");
        btn1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn1MouseClicked(e);
            }
        });

        //---- btn4 ----
        btn4.setText("4");
        btn4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn4MouseClicked(e);
            }
        });

        //---- btn6 ----
        btn6.setText("6");
        btn6.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn6MouseClicked(e);
            }
        });

        //---- btn7 ----
        btn7.setText("7");
        btn7.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn7MouseClicked(e);
            }
        });

        //---- btn8 ----
        btn8.setText("8");
        btn8.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn8MouseClicked(e);
            }
        });

        //---- btn9 ----
        btn9.setText("9");
        btn9.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn9MouseClicked(e);
            }
        });

        //---- btn5 ----
        btn5.setText("5");
        btn5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn5MouseClicked(e);
            }
        });

        //---- btn2 ----
        btn2.setText("2");
        btn2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn2MouseClicked(e);
            }
        });

        //---- btn3 ----
        btn3.setText("3");
        btn3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn3MouseClicked(e);
            }
        });

        //---- btnLog ----
        btnLog.setText("log");
        btnLog.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnLogMouseClicked(e);
            }
        });

        //---- btn0 ----
        btn0.setText("0");
        btn0.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btn0MouseClicked(e);
            }
        });

        //---- btnPoint ----
        btnPoint.setText(".");
        btnPoint.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnPointMouseClicked(e);
            }
        });

        //---- btnDel ----
        btnDel.setText("del");
        btnDel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnDelMouseClicked(e);
            }
        });

        //---- btnSub ----
        btnSub.setText("-");
        btnSub.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnSubMouseClicked(e);
            }
        });

        //---- btnAdd ----
        btnAdd.setText("+");
        btnAdd.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnAddMouseClicked(e);
            }
        });

        //---- btnCalculate ----
        btnCalculate.setText("=");
        btnCalculate.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnCalculateMouseClicked(e);
            }
        });

        //---- btnMul ----
        btnMul.setText("*");
        btnMul.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnMulMouseClicked(e);
            }
        });

        //---- btnDiv ----
        btnDiv.setText("/");
        btnDiv.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnDivMouseClicked(e);
            }
        });

        //---- btnPow ----
        btnPow.setText("pow");
        btnPow.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnPowMouseClicked(e);
            }
        });

        //---- btnDel2 ----
        btnDel2.setText("C");
        btnDel2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnDel2MouseClicked(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(26, 26, 26)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtIn)
                        .addComponent(txtOut)
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                            .addComponent(btnLog)
                                            .addGap(18, 18, 18)
                                            .addComponent(btn0)
                                            .addGap(18, 18, 18)
                                            .addComponent(btnPoint))
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                            .addGroup(contentPaneLayout.createParallelGroup()
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                    .addComponent(btn1)
                                                    .addGap(18, 18, 18)
                                                    .addComponent(btn2))
                                                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                                    .addComponent(btn7)
                                                    .addGap(18, 18, 18)
                                                    .addComponent(btn8))
                                                .addGroup(contentPaneLayout.createSequentialGroup()
                                                    .addComponent(btn4)
                                                    .addGap(18, 18, 18)
                                                    .addComponent(btn5)))
                                            .addGap(18, 18, 18)
                                            .addGroup(contentPaneLayout.createParallelGroup()
                                                .addComponent(btn3)
                                                .addComponent(btn6)
                                                .addComponent(btn9, GroupLayout.Alignment.TRAILING))))
                                    .addGap(18, 18, 18)
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(btnDel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btnSub, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btnAdd, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btnCalculate, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(btnMul)
                                    .addGap(18, 18, 18)
                                    .addComponent(btnDiv)
                                    .addGap(18, 18, 18)
                                    .addComponent(btnPow)
                                    .addGap(18, 18, 18)
                                    .addComponent(btnDel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGap(11, 11, 11)))
                    .addContainerGap(40, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(14, 14, 14)
                    .addComponent(txtIn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(txtOut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(btnMul, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnDiv, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(btnPow, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnDel2, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(btnDel, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(btnSub, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(btnAdd, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(btnCalculate, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(btn8, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btn7, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
                                    .addGap(18, 18, 18)
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(btn4, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btn5, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
                                    .addGap(18, 18, 18)
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(btn1, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btn2, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)))
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(btn9, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGap(18, 18, 18)
                                    .addComponent(btn6, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(btn3, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)))
                            .addGap(18, 18, 18)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(btnLog, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                                .addComponent(btn0, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                                .addComponent(btnPoint, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))))
                    .addGap(12, 12, 12))
        );
        pack();
        setLocationRelativeTo(getOwner());

        //======== this2 ========
        {
            Container this2ContentPane = this2.getContentPane();

            //---- button4 ----
            button4.setText("1");

            //---- button8 ----
            button8.setText("4");

            //---- button14 ----
            button14.setText("6");

            //---- button15 ----
            button15.setText("7");

            //---- button16 ----
            button16.setText("8");

            //---- button17 ----
            button17.setText("9");

            //---- button18 ----
            button18.setText("5");

            //---- button5 ----
            button5.setText("2");

            //---- button6 ----
            button6.setText("3");

            GroupLayout this2ContentPaneLayout = new GroupLayout(this2ContentPane);
            this2ContentPane.setLayout(this2ContentPaneLayout);
            this2ContentPaneLayout.setHorizontalGroup(
                this2ContentPaneLayout.createParallelGroup()
                    .addGroup(this2ContentPaneLayout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(this2ContentPaneLayout.createParallelGroup()
                            .addGroup(this2ContentPaneLayout.createSequentialGroup()
                                .addComponent(button4)
                                .addGap(18, 18, 18)
                                .addComponent(button5)
                                .addGap(18, 18, 18)
                                .addComponent(button6))
                            .addGroup(GroupLayout.Alignment.TRAILING, this2ContentPaneLayout.createSequentialGroup()
                                .addComponent(button15)
                                .addGap(18, 18, 18)
                                .addComponent(button16)
                                .addGap(18, 18, 18)
                                .addComponent(button17))
                            .addGroup(this2ContentPaneLayout.createSequentialGroup()
                                .addComponent(button8)
                                .addGap(18, 18, 18)
                                .addComponent(button18)
                                .addGap(18, 18, 18)
                                .addComponent(button14)))
                        .addContainerGap(66, Short.MAX_VALUE))
            );
            this2ContentPaneLayout.setVerticalGroup(
                this2ContentPaneLayout.createParallelGroup()
                    .addGroup(GroupLayout.Alignment.TRAILING, this2ContentPaneLayout.createSequentialGroup()
                        .addGap(112, 112, 112)
                        .addGroup(this2ContentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                            .addComponent(button17, GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                            .addComponent(button16, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button15, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(this2ContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(button8, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addComponent(button14, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addComponent(button18, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(this2ContentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(button4, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addComponent(button5, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                            .addComponent(button6, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
                        .addGap(77, 77, 77))
            );
            this2.pack();
            this2.setLocationRelativeTo(this2.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton btn1;
    private JButton btn4;
    private JButton btn6;
    private JButton btn7;
    private JButton btn8;
    private JButton btn9;
    private JButton btn5;
    private JButton btn2;
    private JButton btn3;
    private JButton btnLog;
    private JButton btn0;
    private JButton btnPoint;
    private JButton btnDel;
    private JButton btnSub;
    private JButton btnAdd;
    private JButton btnCalculate;
    private JButton btnMul;
    private JButton btnDiv;
    private JButton btnPow;
    private JTextField txtOut;
    private JTextField txtIn;
    private JButton btnDel2;
    private JFrame this2;
    private JButton button4;
    private JButton button8;
    private JButton button14;
    private JButton button15;
    private JButton button16;
    private JButton button17;
    private JButton button18;
    private JButton button5;
    private JButton button6;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
